﻿var table;
$(document).ready(function () {
    $("#rightUser").addClass("active");
    table = $('#RoleListDIV').dataTable({
        "bServerSide": true, //这个用来指明是通过服务端来取数据
        "sAjaxSource": "/Role/RoleList_GetData", //这个是请求的地址
        // "bProcessing": true, // 是否显示取数据时的那个等待提示
        "iDisplayLength": 15,
        "bFilter": false,
        "aoColumns": [
            { "mDataProp": "RoleID", "sTitle": "RoleID", "sDefaultContent": "" },
            { "mDataProp": "RoleName", "sTitle": "RoleName", "sDefaultContent": "" },
            { "mDataProp": "IsActive", "sTitle": "Is Active", "sDefaultContent": "" }
        ],
        "aoColumnDefs": [
            {
                "targets": [0], // 目标列位置，下标从0开始
                "visible": false,
                "data": "RoleName" // 数据列名
            },
            {
                "targets": [1], // 目标列位置，下标从0开始
                "data": "RoleName",
                "render": function (data, type, full) { // 返回自定义内容
                    return "<a href='javascript:void(0)' onclick=\"javascript:updatainfo('" + full.RoleID + "')\" class='Link'>" + data + "</a>";
                }
            },
             {
                 "targets": [2], // 目标列位置，下标从0开始
                 "data": "IsActive", // 数据列名
                 "render": function (data, type, full) { // 返回自定义内容
                     if (full.IsActive == 0) {
                         return "Active";
                     }
                     else {
                         return "InActive";
                     }
                 }
             }
        ],
        "fnServerParams": function (aoData) { //查询条件
            aoData.push(
                {
                    "name": "Rolename", "value": $("#txt_Rolename").val()
                },
            { "name": "isactive", "value": $("#isActive").val() }

            );
        },
        "bLengthChange": false,
        "sPaginationType": "full_numbers",
        "fnInitComplete": function (data) {

        }
    });

    $('#delBut').click(function () {
        var selectdata = $("#RoleListDIV").DataTable().rows('.selected').data();
        var selectleng = selectdata.length;

        if (selectleng > 0) {
            var answer = confirm("您确认要删除记录吗");
            if (answer) {
                var idlist = "";
                for (var i = 0; i < selectleng; i++) {
                    idlist += selectdata[i].RoleID + ",";
                }
                idlist = idlist.substring(0, idlist.length - 1);
                alert(idlist);
                $.ajax({
                    type: "post",
                    url: "/Role/Delinfo",
                    data: { "idlist": idlist },
                    success: function (data) {
                        table.fnDraw();
                    }
                });
            }
        }
    });
    $("#addBut").click(function () {
        location.href = "/Role/RoleAdd";
    });


    /* Get the rows which are currently selected */
    function fnGetSelected(oTable) {
        var aReturn = new Array();
        var aTrs = table.fnGetNodes();
        for (var i = 0; i < aTrs.length; i++) {
            if ($(aTrs[i]).hasClass('row_selected')) {
                aReturn.push(aTrs[i]);
            }
        }
        return aReturn;
    }

    $("#Searchbtn").bind("click", function () { //按钮 触发table重新请求服务器
        table.fnDraw();
    });

});

function updatainfo(roleID) {
    if (roleID != undefined) {
        location.href = "/Role/RoleUpdate/" + roleID;
    } else {
        var selectdata = $("#RoleListDIV").DataTable().rows('.selected').data();
        var selectleng = selectdata.length;
        if (selectleng == 1) {
            location.href = "/Role/RoleUpdate/" + selectdata[0].RoleID;
        } else {
            alert("只能选择一条数据");
        }
    }

}
function Deleteinfo(roleID) {
    $.confirm({
        title: '系统提示',
        content: '您确认要删除记录吗？',
        confirm: function () {
            $.ajax({
                type: "post",
                url: "/Role/Delinfo",
                data: { "roleID": roleID },
                success: function (data) {
                    table.fnDraw();
                }
            });
        },
        cancel: function () {
        }
    });
}
